using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using padnug.Domain;
using PadnugDomain = padnug.Domain;
using PadnugService;
namespace padnug.Controllers
{
    public class AdminController : Controller
    {
        
        private PadnugEventsService svc;
        private PadnugCalendarService calendarService;
        public AdminController()
        {
            svc = new PadnugEventsService();
            calendarService = new PadnugCalendarService();
        }
        //
        // GET: /Admin/

        public ActionResult Index(int? skip, int? take)
        {
            skip = skip.HasValue ? skip : 0;
            take = take.HasValue ? take : 15;
            ViewData.Model = svc.GetEvents(skip, take);
            return View();
        }

        public ActionResult SaveEvent()
        {
            ViewData.Model = new padnug.Domain.Event();
            return View();
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SaveEvent(Event padnugEvent)
        {
            ViewData.Model = padnugEvent;
            //var saved = svc.SaveEvent(padnugEvent);
            return View(); 
        }

        [Authorize]
        public ActionResult UpdateEvent(int eventId)
        {
            var padnugEvent = svc.GetEvent(eventId);
            ViewData.Model = padnugEvent;
            return View();
        }

        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateEvent(Event padnugEvent)
        {
            ViewData.Model = padnugEvent;
            var result = svc.UpdateEvent(padnugEvent);
            return View();
        }

        [Authorize]
        public ActionResult DeleteEvent(int eventId)
        {
            bool result = svc.DeleteEvent(eventId);
            return Index(null, null);
        }
        public ActionResult GetCalendarEvents()
        {
            IList<PadnugDomain.CalendarEvent> events = new List<PadnugDomain.CalendarEvent>();

            if (System.Web.HttpContext.Current.Session["CalendarEvents"] == null)
            {
                events = calendarService.GetAllEvents();
                System.Web.HttpContext.Current.Session.Add("CalendarEvents", events);
            }
            else
            {
                events = (List<PadnugDomain.CalendarEvent>)System.Web.HttpContext.Current.Session["CalendarEvents"];
            }
            return Json(events);
        }
    }
}
